package com.clearliang.mobframe.utils;

import android.util.Base64;

import com.alibaba.fastjson.JSONObject;
import com.mob.mobapi.API;
import com.mob.mobapi.APICallback;
import com.mob.mobapi.MobAPI;
import com.mob.mobapi.apis.BankCard;
import com.mob.mobapi.apis.Calendar;
import com.mob.mobapi.apis.IPStore;
import com.mob.mobapi.apis.Mobile;
import com.mob.mobapi.apis.TrainTickets;
import com.mob.mobapi.apis.UserCenter;
import com.mob.mobapi.apis.Weather;
import com.mob.tools.utils.ResHelper;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by ClearLiang on 2019/1/17
 * <p>
 * Function :   Mob API 接口工具类
 */
public class ApiUtil {
    private static class Parameter {
        public static final int BankCardInfo = 1;   //银行卡
        public static final int BoxOffice = 2;   //票房
        public static final int Calendar = 3;   //日历
        public static final int Car = 4;   //汽车品牌
        public static final int Cook = 5;   //菜单列表
        public static final int Custom = 6;   //自定义API
        public static final int Dictionary = 7;   //新华字典
        public static final int Domesticmetal = 8;   //贵金属
        public static final int Dream = 9;   //周公解梦
        public static final int Environment = 10;  //空气质量
        public static final int Exchange = 11;  //汇率
        public static final int Flight = 12;  //航班
        public static final int Footballleague = 13;  //足球联赛
        public static final int Globalstock = 14;  //全球股指
        public static final int Gold = 15;  //黄金数据查询（期货、现货）
        public static final int Health = 16;  //健康知识
        public static final int History = 17;  //历史上的今天
        public static final int Horoscope = 18;  //八字
        public static final int Idcard = 19;  //身份证
        public static final int Idiom = 20;  //成语
        public static final int Iktoken = 21;  //词库分词
        public static final int Ipstore = 22;  //IP地址
        public static final int Laohuangli = 23;  //老黄历
        public static final int Lottery = 24;  //中国彩票开奖结果
        public static final int Marriage = 25;  //婚姻匹配
        public static final int Mobile = 26;  //手机号码归属地
        public static final int Mobilelucky = 27;  //手机号凶吉
        public static final int Oilprice = 28;  //今日油价
        public static final int Postcode = 29;  //邮政编码
        public static final int Silver = 30;  //白银数据查询（期货，现货）
        public static final int Station = 31;  //基站详情
        public static final int Tiku = 32;  //驾考题库
        public static final int Traintickets = 33;  //火车票
        public static final int Ucache = 34;  //K-V存取
        public static final int Usercenter = 35;  //用户系统
        public static final int Weather = 36;  //天气
        public static final int Wxarticle = 37;  //微信精选
    }

    public static void getBankCard(String cardNumber, final APICallBack2 apiCallback2) {
        BankCard api = ResHelper.forceCast(MobAPI.getAPI(BankCard.NAME));
        api.queryBankCard(cardNumber.trim(), new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getWeather(String cityName, final APICallBack2 apiCallback2) {
        Weather api = (Weather) MobAPI.getAPI(Weather.NAME);
        api.queryByCityName(cityName.trim(), new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getMobile(String phoneNumber, final APICallBack2 apiCallback2) {
        Mobile mobileApi = (Mobile) MobAPI.getAPI(Mobile.NAME);
        mobileApi.phoneNumberToAddress(phoneNumber.trim(), new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getCalendar(String date, final APICallBack2 apiCallback2) {
        //格式：1990-01-01
        Calendar calendarApi = (Calendar) MobAPI.getAPI(Calendar.NAME);
        calendarApi.queryCalendar(date.trim(), new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getIpstore(String ip, final APICallBack2 apiCallback2) {
        IPStore ipStoreApi = (IPStore) MobAPI.getAPI(IPStore.NAME);
        ipStoreApi.queryIPStore(ip.trim(), new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getTrainTicket(String start, String end, final APICallBack2 apiCallback2) {
        TrainTickets trainTicketsApi = (TrainTickets) MobAPI.getAPI(TrainTickets.NAME);
        trainTicketsApi.queryByStationToStation(start, end, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getTrainTicket(String trainNo, final APICallBack2 apiCallback2) {
        TrainTickets trainTicketsApi = (TrainTickets) MobAPI.getAPI(TrainTickets.NAME);
        trainTicketsApi.queryByTrainNo(trainNo, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onError(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemRegister(String username, String password, String email, final APICallBack2 apiCallback2) {
        UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
        userCenterApi.register(username, password, email, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onSuccess(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemLogin(String username, String password, final APICallBack2 apiCallback2) {
        UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
        userCenterApi.login(username, password, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onSuccess(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemChangePwd(String username, String oldPassword, String newPassword, final APICallBack2 apiCallback2) {
        UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
        userCenterApi.changePassword(username, oldPassword, newPassword, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onSuccess(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemChangePwd(String username, String oldPassword, String newPassword, int code, final APICallBack2 apiCallback2) {
        UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
        userCenterApi.changePassword(username, oldPassword, newPassword, code, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onSuccess(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemResetPwd(String username, final APICallBack2 apiCallback2) {
        UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
        userCenterApi.forgotPassword(username, new APICallback() {
            @Override
            public void onSuccess(API api, int action, Map<String, Object> map) {
                apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
            }

            @Override
            public void onError(API api, int action, Throwable throwable) {
                apiCallback2.onSuccess(throwable.getMessage(), api, action);
            }
        });
    }

    public static void getSystemUserData(String token, String uid, String item, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.queryData(token, uid, encodeItem, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public static void getSystemUserProfile(String uid, String item, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.queryProfile(uid, encodeItem, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    public static void getSystemAddUserData(String token, String uid, String item, String value, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            String encodeValue = encodeData(value);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.putData(token, uid, encodeItem, encodeValue, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void getSystemAddUserProfile(String token, String uid, String item, String value, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            String encodeValue = encodeData(value);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.putProfile(token, uid, encodeItem, encodeValue, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void getSystemDeleteUserData(String token, String uid, String item, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.delData(token, uid, encodeItem, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


    }

    public static void getSystemDeleteUserProfile(String token, String uid, String item, final APICallBack2 apiCallback2) {
        try {
            String encodeItem = encodeData(item);
            UserCenter userCenterApi = (UserCenter) MobAPI.getAPI(UserCenter.NAME);
            userCenterApi.delProfile(token, uid, encodeItem, new APICallback() {
                @Override
                public void onSuccess(API api, int action, Map<String, Object> map) {
                    apiCallback2.onSuccess(String.valueOf(JSONObject.toJSON(map)), api, action);
                }

                @Override
                public void onError(API api, int action, Throwable throwable) {
                    apiCallback2.onSuccess(throwable.getMessage(), api, action);
                }
            });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    //用户资料项和用户数据进行base64编码(此部分必须是base64编码,如有需要,在编码前可自行加密)
    private static String encodeData(String data) throws UnsupportedEncodingException {
        //进行BASE64编码,URL_SAFE/NO_WRAP/NO_PADDING
        return new String(Base64.encode(data.getBytes("utf-8"), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING), "utf-8");
    }

    //解析用户资料项和用户数据
    private static String decodeData(String data) throws UnsupportedEncodingException {
        //进行BASE64解码,URL_SAFE/NO_WRAP/NO_PADDING
        return new String(Base64.decode(data.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING), "utf-8");
    }

    //查询用户信息时，Base64解析返回的信息，转化为JSON格式
    public interface APICallBack2 {
        void onSuccess(String successString, API api, int action);

        void onError(String errorString, API api, int action);
    }

}
